<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.longtop.com/intelliweb" prefix="e"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
		<title>Insert title here</title>
	</head>

	<e:View id="view">
		<script type="text/javascript" src="<c:url value='/scripts/global.js'/>"></script>
		<script type="text/javascript" src="<c:url value='/scripts/skin.js'/>"></script>
	    <script type="text/javascript">SkinUtil.getSkin();</script>
	    <script type="text/javascript">SkinUtil.getModuleSkin();</script>
    
		<div id="Invisual Controls" style="min-height: 40px; background-color: LemonChiffon;" >
			<e:Dataset id="Dataset_DsAcctAssign" datasetDefinition="DatasetDefinition_fp.dsd" datasetRef="Dataset_DsAcctAssign" pageSize="-1" onAfterScroll="Dataset_DsAcctAssign_onAfterScroll(dataset)">
				<e:Field name="assignId" dataType="string" label="分配ID" maxLength="32" visible="false"/>
				 <e:Field name="acctId" dataType="string" label="账户ID" maxLength="100" visible="false"/>
				 <e:Field name="startDate" dataType="date" label="开始日期" maxLength="7" />
				 <e:Field name="endDate" dataType="date" label="结束日期" maxLength="7" />
				 <e:Field name="remark" dataType="string" label="备注" maxLength="400" />
			</e:Dataset>
			<e:Dataset id="Dataset_DsAcctAssignMx" datasetDefinition="DatasetDefinition_fp.dsd" datasetRef="Dataset_DsAcctAssignMx" pageSize="-1" >
				<e:Field name="id" dataType="string" label="id" maxLength="32" visible="false"/>
				 <e:Field name="assignId" dataType="string" label="分配ID" maxLength="100" visible="false"/>
				 <e:Field name="userId" dataType="string" label="员工ID" maxLength="32" readOnly="true" />
				 <e:Field name="userName" dataType="string" label="员工姓名" maxLength="100" dropDown="LookupDropDown_User"/>
				 <e:Field name="orgName" dataType="string" label="员工所在机构" maxLength="100" readOnly="true"/>
				 <e:Field name="rate" dataType="bigdecimal" label="业绩比率" maxLength="5" />
				 <e:Field name="createTime" dataType="date" label="创建时间" maxLength="7" readOnly="true"/>
				 <e:Field name="createUserId" dataType="string" label="创建人ID" maxLength="32" visible="false"/>
				 <e:Field name="createUserName" dataType="string" label="创建人名称" maxLength="100" readOnly="true"/>
				 <e:Field name="createOrgId" dataType="string" label="创建机构ID" maxLength="32" visible="false"/>
				 <e:Field name="createOrgName" dataType="string" label="创建机构名称" maxLength="100" readOnly="true"/>
				 <e:Field name="updateUserName" dataType="string" label="修改人" maxLength="100" readOnly="true"/>
				 <e:Field name="updateTime" dataType="date" label="修改时间" maxLength="7" readOnly="true"/>
			</e:Dataset>
			
			<e:Command id="Command_Search" action="com.longtop.perform.fp.web.DsAcctAssignListAction" method="searchAssignDetail" submitForm="true" submitFormScope="all" validateRequired="true">
				<e:DatasetInfo name="DatasetInfo2" dataset="Dataset_DsAcctAssign" alwayRefreshClient="true" submitType="all"></e:DatasetInfo>
				<e:DatasetInfo name="DatasetInfo3" dataset="Dataset_DsAcctAssignMx" alwayRefreshClient="true" submitType="all"></e:DatasetInfo>
				<e:Parameter name="assignId" value=""></e:Parameter>
			</e:Command>
			<e:Command id="Command_Load" action="com.longtop.perform.fp.web.DsAcctAssignListAction" method="searchAssignDetail" showHintInfoOnSuccess="false">
				<e:DatasetInfo name="DatasetInfo4" dataset="Dataset_DsAcctAssignMx" alwayRefreshClient="true"></e:DatasetInfo>
				<e:Parameter name="assignId"></e:Parameter>
			</e:Command>
			
			<e:Command id="Command_Save" action="com.longtop.perform.fp.web.DsAcctAssignListAction" method="saveAssign" submitForm="true" validateRequired="true" >
				<e:DatasetInfo name="DatasetInfo1" dataset="Dataset_DsAcctAssign" alwayRefreshClient="false" submitType="changes"></e:DatasetInfo>
				<e:DatasetInfo name="DatasetInfo3" dataset="Dataset_DsAcctAssignMx" alwayRefreshClient="true" submitType="changes"></e:DatasetInfo>
			</e:Command>
			
			<e:LookupDropDown id="LookupDropDown_Org" path="/jsp/framework/sys/org/orgDropdown.jsp"	windowWidth="600" windowHeight="400" 
					autoDropDown="true" fixed="true" onSelect="LookupDropDown_Org_onSelect(dropdown,iframe,text)" windowCaption="选择机构">
			</e:LookupDropDown>
			
			<e:LookupDropDown id="LookupDropDown_User" path="/jsp/framework/sys/user/userDropdown.jsp"	windowWidth="600" windowHeight="400" 
					autoDropDown="true" fixed="true" onSelect="LookupDropDown_User_onSelect(dropdown,iframe,text)" windowCaption="选择员工">
			</e:LookupDropDown>
		</div>

		<body style="overflow:auto">
			<e:Page id="ePage" action="com.longtop.perform.fp.web.DsAcctAssignListAction" style="min-height: 80px; width: 100%;" onAfterLoadPage="ePage_onAfterLoadPage()">
				<table style="width: 98%;" border="0" cellpadding="0" cellspacing="0" width="100%" align="center">
					<tr>
						<td valign="top" width="100%">
							<e:GroupBox id="GroupBox1" caption="分配业绩" style="height: 100%; width:100%;">
								<table style="text-align: left; width: 100%;" border="0" cellpadding="3" cellspacing="3" >
									<tr>
										<td>
											<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0">
												<tr>
													<td height="27px" width="1%">
														<e:RecordNavigator id="RecordNavigator_CustAssign" dataset="Dataset_DsAcctAssign" showNavigatorButton="false">
															<e:RecordButton id="RecordButton_insert" label="新增" onClick="insertCustAssign()" icon="/images/add.gif"></e:RecordButton>
															<e:RecordButton id="RecordButton_delete" label="删除" onClick="deleteCustAssign()" icon="/images/delete.gif"></e:RecordButton>
															<e:RecordButton id="RecordButton_save" label="保存" icon="/images/submit.gif" onClick="RecordButton_save_onClick()"></e:RecordButton>
														</e:RecordNavigator>
													</td>
												</tr>
											</table>
										</td>
									</tr>
									<tr>
										<td>
											<e:Grid id="Grid_CustAssign" style="width: 100%; height: 150px;" showIndex="true" dataset="Dataset_DsAcctAssign" onClick="Grid_Assign_onClick()">
											</e:Grid>
										</td>
									</tr>
								</table>
							</e:GroupBox>
						</td>
					</tr>
					<tr>
						<td>
							<e:GroupBox id="GroupBox2" caption="业绩分配详情" style="height: 100%; width:100%;">
								<table style="text-align: left; width: 100%;" border="0" cellpadding="3" cellspacing="3" >
									<tr>
										<td>
											<table style="text-align: left; width: 100%;" border="0" cellpadding="0" cellspacing="0">
												<tr>
													<td height="27px" width="1%">
														<e:RecordNavigator id="RecordNavigator_CustAssignDetail" dataset="Dataset_DsAcctAssignMx" showNavigatorButton="false">
															<e:RecordButton id="RecordButton_insert" label="新增" onClick="insertRecord" icon="/images/add.gif"></e:RecordButton>
															<e:RecordButton id="RecordButton_delete" label="删除" onClick="deleteRecord" icon="/images/delete.gif"></e:RecordButton>
															<e:RecordButton id="RecordButton_save" label="保存" icon="/images/submit.gif" onClick="RecordButton_save_onClick()"></e:RecordButton>
														</e:RecordNavigator>
													</td>
												</tr>
											</table>
										</td>
									</tr>
									<tr>
										<td>
											<e:Grid id="Grid_CustAssignDetail" style="width: 100%; height: 150px;" showIndex="true" dataset="Dataset_DsAcctAssignMx" >
											</e:Grid>
										</td>
									</tr>
								</table>
							</e:GroupBox>
						</td>
					</tr>
			    </table>
			</e:Page>
		</body>
	</e:View>
</html>	

<script type="text/javascript">
	var isedit=false;
	//init
	function ePage_onAfterLoadPage(){
		var record_data=$J("Dataset_DsAcctAssign").getCurrentRecord();
		if(record_data!=null){
			Command_Search.parameters.setValue("assignId",record_data.getValue("assignId"));
			Command_Search.execute();
		}
	}
	
	//添加
	function insertCustAssign(){
		if(isedit){
			alert("请先保存后再操作");
			return false;
		}
		else{
			isedit=true;
			$J("Dataset_DsAcctAssign").insertRecord();
			Button_Load_onClick();
		}
	}
	//删除
	function deleteCustAssign(){
		var record_data=$J("Dataset_DsAcctAssign").getCurrentRecord();
		if(record_data.getValue("assignId")==null){
			$J("Dataset_DsAcctAssign").deleteRecord();
			isedit=false;
		}else{
			if(isedit){
				alert("请先保存后再操作");
				return false;
			}
			else{
				$J("Dataset_DsAcctAssign").deleteRecord();
				Button_Load_onClick();
				isedit=true;
			}
		}
	}
	
	//单击记录,确定主键是否可编辑
	function Grid_Assign_onClick(){
		var element = event.srcElement;
		if(element.isDataCell){
			if(element.isDataCell){
		        var currentRecord = element.record;
		        if(currentRecord.state == Constant.RECORD.STATE_NEW || currentRecord.state == Constant.RECORD.STATE_INSERT) {
	          		currentRecord.dataset.getField("startDate").setReadOnly(false);
	          	} else{
			        currentRecord.dataset.getField("startDate").setReadOnly(true);
	       		}
			}
		}
	}
	//读取
	function Button_Load_onClick(){
		if($J("Dataset_DsAcctAssign")!=null){
			var record_data=$J("Dataset_DsAcctAssign").getCurrentRecord();
			if(record_data!=null){
				Command_Load.parameters.setValue("assignId",record_data.getValue("assignId"));
			    Command_Load.execute();
			}
		}
		
	}
	//Text选择所属机构
	function LookupDropDown_Org_onSelect(dropdown,iframe,text){
		var dataset = $J(iframe.Dataset_Org);
		var node = $J(iframe.DataTree_Org).getCurrentNode();
		var record_data=$J("Dataset_DsAcctAssign").getCurrentRecord();
		if(dataset && node!=null){//除去根节点
		    var record = dataset.getCurrentRecord();
		    if(typeof record != "undefined" && record != null){
		    	record_data.setValue("fitOrgId",record.getValue("orgId"));
		    	record_data.setValue("fitOrgName",record.getValue("orgName"));
		    }
		}
		if(dataset && node!=null && node.level == 1){//清空
			record_data.setValue("fitOrgId",record.getValue(""));
		    record_data.setValue("fitOrgName",record.getValue("orgName"));
		}
	}
	
	//选择员工
	function LookupDropDown_User_onSelect(dropdown,iframe,text){
		var dataset = $J(iframe.Dataset_User);
		var record_data=$J("Dataset_DsAcctAssignMx").getCurrentRecord();
		var record_assignData=$J("Dataset_DsAcctAssign").getCurrentRecord();
		if(dataset){
		    var record = dataset.getCurrentRecord();
		    if(typeof record != "undefined" && record != null){
		    	var records=$J("Dataset_DsAcctAssignMx").records;
		    	for(var i=0;i<records.length;i++){
					var record_dataone=records.get(i);
					if(record_dataone.getValue("userId")==record.getValue("userId")){
						alert("该用户已分配,请选择其它用户或直接修改");
						return false;
					}
				}
		    	record_data.setValue("userId",record.getValue("userId"));
		    	record_data.setValue("userName",record.getValue("userName"));
		    	record_data.setValue("orgName",record.getValue("orgName"));
		    	record_data.setValue("assignId",record_assignData.getValue("assignId"));
		    }
		}
	}
	
	//保存
	function RecordButton_save_onClick(){
		if(isChecked()){
			Command_Save.execute();
			isedit=false;
			Button_Load_onClick();
		}
	}
	
	function Dataset_DsAcctAssign_onAfterScroll(dateset){
		Button_Load_onClick();
	}
	
	function isChecked(){
		var rates =0;
		var record_datas=$J("Dataset_DsAcctAssignMx").records;
		for(var i=0;i<record_datas.length;i++){
			var record_data=record_datas.get(i);
			rates=rates+record_data.getValue("rate");
		}
		if(rates<=100){
			return true;
		}else{
			alert("分配比例不得大于100");
			return false;
		}
		
	}
		
</script>